package de.derhess.projects.pkr.models
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	import mx.events.PropertyChangeEvent;
	
	/**
	 * released under MIT License (X11)
	 * http://www.opensource.org/licenses/mit-license.php
	 * 
	 * Class  YouTube Player for the Flash Plattform
	 * 
	 * @author Florian Weil [derhess.de, Deutschland]
	 * @see http://blog.derhess.de
	 */
	[Bindable]
	public class PercentageSum extends EventDispatcher
	{
		//--------------------------------------------------------------------------
		//
		//  Class variables
		//
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//
		//  Variables
		//
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//
		//  Initialization
		//
		//--------------------------------------------------------------------------
		public function PercentageSum(percentValue:int,baseVal:Number)
		{
			percentage = percentValue;
			baseValue = baseVal;
			_sum = percentage * baseValue;
			
			this.addEventListener(PropertyChangeEvent.PROPERTY_CHANGE,handleChange);
		}
		
		
		
		//--------------------------------------------------------------------------
		//
		//  Properties
		//
		//--------------------------------------------------------------------------
		public var percentage:int = 0;
		public var baseValue:Number = 0;
		
		/** @private */
		private var _sum:Number = 0;
		/**
		 *
		 */
		[Bindable(event="PercentageSum_Changed")]
		public function get sum():Number
		{
			return _sum;    
		}
		
		private function set sum(value:Number):void
		{
			_sum = value;
			dispatchEvent(new Event("PercentageSum_Changed"));
		}

		//--------------------------------------------------------------------------
		//
		//  Additional getters and setters
		//
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		public override function toString():String 
		{
			return "[PercentageSum] : " + percentage + " * " + baseValue + " = " + _sum;	
		}
		
		
		//--------------------------------------------------------------------------
		//
		//  API
		//
		//--------------------------------------------------------------------------
		
		
		
		//--------------------------------------------------------------------------
		//
		//  Eventhandling
		//
		//--------------------------------------------------------------------------
		private function handleChange(e:PropertyChangeEvent):void
		{
			_sum = (percentage / 100) * baseValue;
			dispatchEvent(new Event("PercentageSum_Changed"));
		}
		
		//--------------------------------------------------------------------------
		//
		//  Broadcasting
		//
		//--------------------------------------------------------------------------
		
		
	}
}